#include "bar.h"

const char* turn = "|/-\\";

void processbar()
{
    int n = 3;
    char arr[Max];
    memset(arr,'\0',sizeof(arr));

    int i = 0;
    while(i<=100)
    {
        printf("[%-100s][%3d%%][%c]\r", arr, i, turn[i%n]);
        arr[i++] = body;
        fflush(stdout);
        usleep(500000);
    }

    printf("\n");
}

char arr[Max] = {0};
void processbar_2(double rate)
{
    static int i = 0;
    int n = strlen(turn);

    if(rate < 1.0)
    {
        arr[(int)rate] = head;
    }
    printf("[\033[1;31;46m%-100s \033[0m][%.1f%%][%c]\r", arr, rate, turn[i%n]);
    fflush(stdout);
    arr[(int)rate] = body;
    if(rate <= 99.0)
    {
        arr[(int)rate + 1] = head;
    }

    if(rate >= 100.0)
    {
        printf("\n");
    }
    i++;
    i %= n;

}

//
////模拟下载场景
//void download(funbar bar)
//{
////模拟下载场景
//    double rate = 0;
//    int total = FILE;
//    int downloaded = 0;
//    srand(time(NULL)^192);
//    while(total)
//    {
//        //下载
//        usleep(10000);
//        int loadspeed = rand()%(1024*1024);
//        total -= loadspeed;
//        if(total < 0)
//        {
//            total = 0;
//        }
//
//        //下载比率
//        downloaded = FILE - total;
//        rate = ((downloaded*1.0)/(FILE))*100.0;
//        //printf("downrate:%.1f%%\n",rate);
//        bar(rate);
//    }
//}
